package com.zhuyh.webgis.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhuyh.webgis.entity.Playdayod;
import com.zhuyh.webgis.entity.Workdayod;
import com.zhuyh.webgis.service.IPlaydayodService;
import com.zhuyh.webgis.service.IWorkdayodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 3247
 * @since 2020-02-09
 */
@RestController
@RequestMapping("/playdayod")
public class PlaydayodController {

    @Autowired
    private IPlaydayodService playdayodService;

    @Autowired
    private IWorkdayodService workdayodService;

    @RequestMapping(value = "/get/{time}")
    public List<List<List<BigDecimal>>> getPlaydayOd(@PathVariable(name = "time") @Valid String time) {

        List<List<List<BigDecimal>>> lists = new ArrayList<>();
        QueryWrapper playQueryWrapper = new QueryWrapper();
        playQueryWrapper.eq("time", time);
        List<Playdayod> list = playdayodService.list(playQueryWrapper);
        list.forEach(playdayod -> {
            List<BigDecimal> startData = new ArrayList<>();
            startData.add(new BigDecimal(playdayod.getStartLng()));
            startData.add(new BigDecimal(playdayod.getStartLat()));

            List<BigDecimal> endData = new ArrayList<>();
            endData.add(new BigDecimal(playdayod.getEndLng()));
            endData.add(new BigDecimal(playdayod.getEndLat()));

            List<List<BigDecimal>> pointData = new ArrayList<>();
            pointData.add(startData);
            pointData.add(endData);

            lists.add(pointData);
        });
        return lists;
    }

    @GetMapping(value = "/search")
    public List<List<List<BigDecimal>>> getData(String work, String time) {
        System.out.println("数据：" + work + ", " + time);
        QueryWrapper workQueryWrapper = new QueryWrapper();
        workQueryWrapper.eq("time", time);
        List<List<List<BigDecimal>>> lists = new ArrayList<>();
        if (StringUtils.equals(work, "work")) {
            //查询work
            List<Workdayod> list = workdayodService.list(workQueryWrapper);
            list.forEach(playdayod -> {
                List<BigDecimal> startData = new ArrayList<>();
                startData.add(new BigDecimal(playdayod.getStartLng()));
                startData.add(new BigDecimal(playdayod.getStartLat()));

                List<BigDecimal> endData = new ArrayList<>();
                endData.add(new BigDecimal(playdayod.getEndLng()));
                endData.add(new BigDecimal(playdayod.getEndLat()));

                List<List<BigDecimal>> pointData = new ArrayList<>();
                pointData.add(startData);
                pointData.add(endData);

                lists.add(pointData);
            });
            return lists;

        } else {
            // 查询play
            List<Playdayod> list = playdayodService.list(workQueryWrapper);
            list.forEach(playdayod -> {
                List<BigDecimal> startData = new ArrayList<>();
                startData.add(new BigDecimal(playdayod.getStartLng()));
                startData.add(new BigDecimal(playdayod.getStartLat()));

                List<BigDecimal> endData = new ArrayList<>();
                endData.add(new BigDecimal(playdayod.getEndLng()));
                endData.add(new BigDecimal(playdayod.getEndLat()));

                List<List<BigDecimal>> pointData = new ArrayList<>();
                pointData.add(startData);
                pointData.add(endData);

                lists.add(pointData);
            });
            return lists;
        }
    }

    @RequestMapping(value = "/get/od/{time}")
    public List<Map<String, Object>> getPlaydayOdGet(@PathVariable(name = "time") @Valid String time) {

        List<Map<String, Object>> lists = new ArrayList<>();


        QueryWrapper playQueryWrapper = new QueryWrapper();
        playQueryWrapper.eq("time", time);
        List<Playdayod> list = playdayodService.list(playQueryWrapper);
        list.forEach(playdayod -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", playdayod.getId());
            Random r = new Random();
            List<BigDecimal> data1 = Arrays.asList(
                    new BigDecimal(playdayod.getStartLng()),
                    new BigDecimal(playdayod.getStartLat()),
                    new BigDecimal(r.nextInt(24)+1));
            map1.put("value",data1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", playdayod.getId() + "1");
            List<BigDecimal> data2 = Arrays.asList(
                    new BigDecimal(playdayod.getEndLng()),
                    new BigDecimal(playdayod.getEndLat()),
                    new BigDecimal(r.nextInt(24)+1));
            map2.put("value",data2);

            lists.add(map1);
            lists.add(map2);
        });
        return lists;
    }

    @RequestMapping(value = "/get/od")
    public List<Map<String, Object>> getAll() {

        List<Map<String, Object>> lists = new ArrayList<>();
        List<Playdayod> list = playdayodService.list();
        list.forEach(playdayod -> {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("name", playdayod.getId());
            List<BigDecimal> data1 = Arrays.asList(
                    new BigDecimal(playdayod.getStartLng()),
                    new BigDecimal(playdayod.getStartLat()),
                    new BigDecimal(playdayod.getTime()));
            map1.put("value",data1);

            Map<String, Object> map2 = new HashMap<>();
            map2.put("name", playdayod.getId() + "1");
            List<BigDecimal> data2 = Arrays.asList(
                    new BigDecimal(playdayod.getEndLng()),
                    new BigDecimal(playdayod.getEndLat()),
                    new BigDecimal(playdayod.getTime()));
            map2.put("value",data2);

            lists.add(map1);
            lists.add(map2);
        });
        return lists;
    }


}

